package br.inf.solutions.apresentacoes.ssp.model.bulletin;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

import br.com.vexillum.model.CommonEntityActivated;
import br.com.vexillum.model.annotations.Validate;
import br.com.vexillum.util.SpringFactory;
import br.inf.solutions.apresentacoes.ssp.control.PublicationController;
import br.inf.solutions.apresentacoes.ssp.control.bulletin.BaseItensBulletinController;
import br.inf.solutions.apresentacoes.ssp.model.Publication;
import br.inf.solutions.apresentacoes.ssp.model.interfaces.IBulletinItem;
import br.inf.solutions.apresentacoes.ssp.model.interfaces.IExistsPerson;

@SuppressWarnings("serial")
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class BulletinItem extends CommonEntityActivated implements
		IBulletinItem, IExistsPerson {

	@Validate(notNull = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "id_publication", nullable = false, updatable = true, unique = false)
	private Publication publication;

	@Validate(notNull = false)
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "id_bulletin", nullable = true, updatable = true, unique = false)
	private Bulletin bulletin;

	@Column(name = "text_item", nullable = true, updatable = true, unique = false, length=2000)
	private String textItem;
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "id_rectified", nullable = true, updatable = true, unique = false)
	private BulletinItem rectifiedItem;

	@Transient
	protected BulletinTopics bulletinTopic;
	
	public BulletinItem() {
	}
	
	public BulletinItem(BulletinTopics bulletinTopic) {
		setBulletinTopic(bulletinTopic);
//		setPublication(getDefaultPublication());
	}
	
	public Publication getPublication() {
		return publication;
	}

	public void setPublication(Publication publication) {
		this.publication = publication;
	}

	public Bulletin getBulletin() {
		return bulletin;
	}

	public void setBulletin(Bulletin bulletin) {
		this.bulletin = bulletin;
	}

	public String getTextItem() {
		return textItem;
	}

	public void setTextItem(String textItem) {
		this.textItem = textItem;
	}

	public BulletinItem getRectifiedItem() {
		return rectifiedItem;
	}

	public void setRectifiedItem(BulletinItem rectifiedItem) {
		this.rectifiedItem = rectifiedItem;
	}

	@Override
	public BulletinTopics getBulletinTopic() {
		return bulletinTopic;
	}

	@Override
	public void setBulletinTopic(BulletinTopics bulletinTopic) {
		this.bulletinTopic = bulletinTopic;
	}
	
	public Publication getDefaultPublication(){
		PublicationController controller = SpringFactory.getController("publicationController", PublicationController.class, null);
		return controller.getDefaultPuplication();
	}
	
	public abstract BaseItensBulletinController<? extends BulletinItem> getControl();
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void afterPublication(){
		BaseItensBulletinController controller = getControl();
		controller.setEntity(this);
		controller.afterPublicate();
	}

}
